﻿/********************************************************************************
** 作者： Mick
** 邮箱： zheng_jinfan@126.com
** 主页： http://www.zhengjinfan.cn
** 创始时间：2016-2-5
** 描述：
**      值对象基类
*********************************************************************************/
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BestEasyCMS.Framework.Domain
{
    /// <summary>
    /// 值对象基类
    /// </summary>
    public abstract class ValueObjectBase
    {
        private readonly List<BusinessRule> _brokenRules = new List<BusinessRule>();

        /// <summary>
        /// 验证
        /// </summary>
        protected abstract void Validate();

        public void ThrowExceptionIfInvalid()
        {
            _brokenRules.Clear();
            Validate();
            if (_brokenRules.Any())
            {
                var issues = new StringBuilder();
                foreach (var businessRule in _brokenRules){ 
                    issues.AppendLine(businessRule.Rule);
                }

                throw new ValueObjectIsInvalidException(issues.ToString());
            }
        }

        protected void AddBrokenRule(BusinessRule businessRule)
        {
            _brokenRules.Add(businessRule);
        }
    }
}
